﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Syndication;
using FeedReader.Shared.Extensions;

namespace FeedReader.Shared
{
    public class Feed
    {
        private System.ServiceModel.Syndication.SyndicationFeed result;

        public string Title { get; set; }
        public string Description { get; set; }
        public DateTimeOffset DateTime {get; set;}
        public string URL { get; set; }
        public List<Post> Posts { get; private set; }
        public Feed(SyndicationFeed result)
        {
            // TODO: Complete member initialization
            this.result = result;
            this.Title = result.Title.Text;
            try
            {
                this.Description = result.Description.Text;
            }
            catch { }
            this.DateTime = result.LastUpdatedTime;
            this.URL = result.Links[0].Uri.OriginalString;
            this.Posts = new List<Post>();

            parseposts();
        }

        private void parseposts(){
            foreach (SyndicationItem item in this.result.Items)
            {
                Post newitem = new Post();
                newitem.Content = item.Content.getString(item.Summary);
                newitem.PublishedTime = item.PublishDate;
                try
                {
                    newitem.Summary = item.Summary.Text;
                }
                catch { }
                newitem.Title = item.Title.Text;

                this.Posts.Add(newitem);
            }
        }
    }

    public class FeedsCollection : System.Collections.Generic.List<Feed>
    {

    }
}
